package game_play_strategy.tools;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Toast;



public class TimerService extends Service 
{
	 private static final String TAG = TimerService.class.getSimpleName();
	private Timer timer;
	int player_1_id, player_2_id;
	String player_1_imei, player_2_imei;
	SqlConnector sqlConnector = new SqlConnector();
	DBAdapter myDBAdapter;

	private TimerTask updateTask = new TimerTask() 
	{
	  private DBAdapter myDBAdapter2;

	@Override
	  public void run() 
	  {
		  Context context = getApplicationContext();
		  this.myDBAdapter2 = new DBAdapter(context);
		  myDBAdapter = myDBAdapter2;
		  runUpdate(context);
		  Log.i(TAG, player_1_imei);
		  Log.i(TAG, player_2_imei);
	    Log.i(TAG, "Timer task doing work");
	  }
	  

	};

	@Override
	public IBinder onBind(Intent intent) {
	  // TODO Auto-generated method stub
	  return null;
	}

	@Override
	public void onCreate() {
	  super.onCreate();
	  Log.i(TAG, "Service creating");
	
	  timer = new Timer("TimerService");
	  timer.schedule(updateTask, 60 * 1000L, 60 * 1000L);
	}

	@Override
	public void onDestroy() {
	  super.onDestroy();
	  Log.i(TAG, "Service destroying");
	  try {
		  myDBAdapter.close();
		} catch (Exception e) {
			// TODO: handle exception
		}	
	  
	  timer.cancel();
	  timer = null;
	}
	
	  @Override
	  public int onStartCommand(Intent intent, int flags, int startId)
	  {		  
		  Bundle bundle = intent.getExtras();
		     player_1_id = bundle.getInt("player_1_id");
		     player_2_id = bundle.getInt("player_2_id");
		     player_1_imei = bundle.getString("player_1_imei");
		     player_2_imei = bundle.getString("player_2_imei");
		  return startId;		
	  }
	  
	  @SuppressWarnings("static-access")
		public boolean runUpdate(Context context)
		{
		  
			
			
			for(int i = 1; i < 5; i++)
			{
				myDBAdapter.openDataBase();
				Cursor h = myDBAdapter.getHero(i);
				// 2) wyslanie bohaterow do MYSQL
				sqlConnector.updateHero(h.getInt(0), player_1_id, player_1_imei, 
						h.getString(3), h.getString(4), h.getString(14), h.getInt(5), h.getInt(6), 
						h.getInt(8), h.getInt(7), h.getInt(10), h.getInt(10), h.getString(13), 
						h.getInt(11), h.getInt(12), context);
				Log.i(TAG, "Timer task doing work");
				myDBAdapter.close();
				myDBAdapter.openDataBase();
				h = myDBAdapter.getHero(i+4);
				// 2) wyslanie bohaterow do MYSQL
				sqlConnector.updateHero(h.getInt(0), player_2_id, player_2_imei, 
						h.getString(3), h.getString(4), h.getString(14), h.getInt(5), h.getInt(6), 
						h.getInt(8), h.getInt(7), h.getInt(10), h.getInt(10), h.getString(13), 
						h.getInt(11), h.getInt(12), context);
				myDBAdapter.close();
				
			}

			sqlConnector.changePlayerTurn(player_1_id);
			sqlConnector.changePlayerTurn(player_2_id);
		
			return true;
		}
}
//}
